using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using Pixysoft.Framework.XSchEmA.Entity;
using Pixysoft.Framework.XSchEmA.Aspect;

namespace Pixysoft.Framework.XSchEmA.Checker
{
    class SchemaNameChecker
    {
        private static SchemaNameChecker instance;
        public static SchemaNameChecker Instance
        {
            get
            {
                lock (typeof(SchemaNameChecker))
                {
                    if (instance == null)
                        instance = new SchemaNameChecker();
                    return instance;
                }
            }
        }

        public List<TableSchema> CheckTable(ISchemaAspect aspect, DatabaseSchema database)
        {
            List<TableSchema> tableList = new List<TableSchema>();

            if (aspect == null)
                return tableList;
            if (database == null)
                return tableList;


            foreach (TableSchema table in database.Tables)
            {
                if (!aspect.IsValidName(table.TableName))
                {
                    tableList.Add(table);
                }
            }

            return tableList;
        }

        public List<ColumnSchema> CheckColumn(ISchemaAspect aspect, DatabaseSchema database)
        {
            List<ColumnSchema> columnList = new List<ColumnSchema>();

            if (aspect == null)
                return columnList;
            if (database == null)
                return columnList;

            foreach (TableSchema table in database.Tables)
            {
                foreach (ColumnSchema column in table.Columns)
                {
                    if (!aspect.IsValidName(column.ColumnName))
                    {
                        columnList.Add(column);
                    }
                }
            }

            return columnList;
        }

        public List<UniqueSchema> CheckUnique(ISchemaAspect aspect, DatabaseSchema database)
        {
            List<UniqueSchema> ulist = new List<UniqueSchema>();

            if (aspect == null)
                return ulist;
            if (database == null)
                return ulist;

            foreach (UniqueSchema unique in database.Uniques)
            {
                if (!aspect.IsValidName(unique.ConsName))
                    ulist.Add(unique);
            }

            return ulist;
        }

        public List<PrimaryKeySchema> CheckPrimaryKey(ISchemaAspect aspect, DatabaseSchema database)
        {
            List<PrimaryKeySchema> list = new List<PrimaryKeySchema>();

            if (aspect == null)
                return list;
            if (database == null)
                return list;

            foreach (PrimaryKeySchema pk in database.PrimaryKeys)
            {
                if (!aspect.IsValidName(pk.ConsName))
                    list.Add(pk);
            }
            return list;
        }

        public List<ForeignKeySchema> CheckForeignKey(ISchemaAspect aspect, DatabaseSchema database)
        {
            List<ForeignKeySchema> list = new List<ForeignKeySchema>();

            if (aspect == null)
                return list;
            if (database == null)
                return list;

            foreach (ForeignKeySchema fk in database.ForeignKeys)
            {
                if (!aspect.IsValidName(fk.ConsName))
                    list.Add(fk);
            }
            return list;
        }

    }
}
